package com.jfdesgagne.traklib.ui.elements {
	import fl.motion.easing.Exponential;
	import fl.transitions.Tween;

	import flash.display.MovieClip;
	import flash.geom.Rectangle;

	/**
	 * @author jdesgagne
	 */
	public class TrakUICursor extends MovieClip {
		protected var __tweenX:Tween;		protected var __tweenY:Tween;
		protected var __tweenScaleX:Tween;		protected var __tweenScaleY:Tween;
		
		public function TrakUICursor() {
			this.x = -this.width;
			this.y = -this.height;
		}
		
		public function move(_goto:Rectangle):void {
			if(this.x != _goto.x) {
				if(this.__tweenX) this.__tweenX.stop();
				this.__tweenX = new Tween(this, "x", Exponential.easeOut, this.x, _goto.x, 0.1, true);			}
			if(this.y != _goto.y) {
				if(this.__tweenY) this.__tweenY.stop();
				this.__tweenY = new Tween(this, "y", Exponential.easeOut, this.y, _goto.y, 0.1, true);
			}
		}
		
		public function grow(_area:Rectangle=null):void {
			if(_area==null) {
				if(this.scaleX != 1) {
					if(this.__tweenScaleX) this.__tweenScaleX.stop();
					this.__tweenScaleX = new Tween(this, "scaleX", Exponential.easeOut, this.scaleX, 1, 0.1, true);
				}
				if(this.scaleY != 1) {
					if(this.__tweenScaleY) this.__tweenScaleY.stop();
					this.__tweenScaleY = new Tween(this, "scaleY", Exponential.easeOut, this.scaleY, 1, 0.1, true);
				}
			} else {
				var scaleX:Number = (_area.width / this.stage.stageWidth)*5;				var scaleY:Number = (_area.height / this.stage.stageHeight)*5;
				if(scaleX>scaleY) scaleY = scaleX; else scaleX = scaleY;
				
				if(this.scaleX != scaleX) {
					if(this.__tweenScaleX) this.__tweenScaleX.stop();
					this.__tweenScaleX = new Tween(this, "scaleX", Exponential.easeOut, this.scaleX, scaleX, 0.1, true);
				}
				if(this.scaleY != scaleY) {
					if(this.__tweenScaleY) this.__tweenScaleY.stop();
					this.__tweenScaleY = new Tween(this, "scaleY", Exponential.easeOut, this.scaleY, scaleY, 0.1, true);
				}
			}
			
		}
		
		public function destroy():void {
			if(this.__tweenScaleX) {
				this.__tweenScaleX.stop();
				this.__tweenScaleX = null;
			} if(this.__tweenScaleY) {
				this.__tweenScaleY.stop();
				this.__tweenScaleY = null;
			} if(this.__tweenY) {
				this.__tweenY.stop();
				this.__tweenY = null;
			} if(this.__tweenX) {
				this.__tweenX.stop();
				this.__tweenX = null;
			}
		}
	}
}
